﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.DBService;
using CP.Utility;
using System.Collections.Specialized;

namespace CP.Repositories.GPM
{
    public class WebpartRepository
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public IList<Webpart> GetAllWebparts()
        {
            return db.ExecuteObject<Webpart>("select * from Webpart order by Webpart_id");
        }

        public Webpart GetWebpartByID(int webpartid)
        {
            IList<Webpart> Webparts = db.ExecuteObject<Webpart>(string.Format(@"select * 
                                                from Webpart
                                        where Webpart_id ='{0}' ", webpartid));
            if (Webparts.Count > 0)
                return Webparts[0];
            return null;
        }

        public Webpart GetWebpartByFormName(string formname)
        {
            return db.ExecuteSingleObject<Webpart>(string.Format(@"select * 
                                                from Webpart
                                        where form_name ='{0}' ", formname));
        }

        public bool DeleteWebpart(IList<int> ids)
        {
            try
            {
                db.BeginTransaction();
                foreach (int id in ids)
                {
                    db.ExecuteNonQuery(string.Format(@"delete Webpart 
                                        where webpart_id = {0} ", id));
                    db.ExecuteNonQuery(string.Format(@"delete Webpart_Right 
                                        where webpart_id = {0} ", id));
                    db.ExecuteNonQuery(string.Format(@"delete BUTTON_LIST 
                                        where webpart_id = {0} ", id));
                    db.ExecuteNonQuery(string.Format(@"delete webpart_right_button 
                                        where webpart_id = {0} ", id));
                    db.ExecuteNonQuery(string.Format(@"delete COLUMN_LIST 
                                        where webpart_id = {0} ", id));
                    db.ExecuteNonQuery(string.Format(@"delete WEBPART_RIGHT_LIST 
                                        where webpart_id = {0} ", id));
                }
                db.Commit();
                return true;
            }

            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }

        public bool AddWebpart(Webpart webpart)
        {
            db.ExecuteNonQuery(string.Format(@"insert into webpart(webpart_id,module_id,
                                        form_name,form_text) 
                                        values({0},{1},'{2}','{3}') ",
                                webpart.webpart_id, webpart.module_id, webpart.form_name, webpart.form_text));
            return true;
        }

        public bool AddWebpartColList(Webpart webpart, NameValueCollection lists)
        {
            string sql = string.Format(@"select * from webpart 
                                              where upper(form_name) = upper('{0}')",
                                                 webpart.form_name);
            IList<Webpart> webparts = db.ExecuteObject<Webpart>(sql);

            if (webparts.Count < 1)
                throw new Exception("组件[" + webpart.form_name + "]不存在,请先初始化组件");

            webpart = webparts[0];
            try
            {
                db.BeginTransaction();

                ColumnListRepository listrep = new ColumnListRepository();
                listrep.DeleteColumn_ListByWebpartID(webpart.webpart_id);
                for (int i = 0; i < lists.Count; i++)
                {
                    Column_List list = new Column_List();
                    list.webpart_id = webpart.webpart_id;
                    list.field_name = lists.GetKey(i);
                    list.list_name = lists.GetValues(i)[0];
                    listrep.AddColumn_List(webpart.webpart_id, list);
                }

                db.Commit();
                return true;
            }
            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }

        public bool AddWebpartButton(Webpart webpart, NameValueCollection actions)
        {
            string sql = string.Format(@"select * from webpart 
                                              where upper(form_name) = upper('{0}')",
                                                 webpart.form_name);
            IList<Webpart> webparts = db.ExecuteObject<Webpart>(sql);

            if (webparts.Count != 1)
                throw new Exception("文件名[" + webpart.form_name + "]不存在");

            //webpart.webpart_id = GetWebpartIDBySeq();
            try
            {
                db.BeginTransaction();
                db.ExecuteNonQuery(string.Format(@"delete button where webpart_id = {0}",
                                                                    webparts[0].webpart_id));

                ButtonRepository btnrep = new ButtonRepository();
                for (int i = 0; i < actions.Count; i++)
                {
                    Button_List btn = new Button_List();
                    btn.webpart_id = webparts[0].webpart_id;
                    btn.action_id = actions.GetKey(i);
                    btn.button_name = actions.GetValues(i)[0];
                    btn.post_url = "0";
                    btnrep.AddButton(webparts[0].webpart_id, btn);
                }
                db.Commit();
                return true;
            }
            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }

        public bool UpdateWebpart(Webpart webpart)
        {
            object ret = db.ExecuteScalar(string.Format(@"select count(*) from dual
                                                    where exists(select null from 
                                                       webpart 
                                              where webpart_id =  {0} )",
                                                    webpart.webpart_id));

            if (0 == Convert.ToInt16(ret))
                throw new Exception("组件不存在");

            db.ExecuteNonQuery(string.Format(@"update webpart set module_id={1},
                                form_name= '{2}',remark='{3}' ,form_text = '{4}' 
                                where webpart_id = {0} ",
                          webpart.webpart_id, webpart.module_id, webpart.form_name,
                          webpart.remark, webpart.form_text));

            return true;
        }

        public int GetWebpartIDBySeq()
        {
            return SequenceGenerator.GetSeqNextValue("seq_webpart");
        }
    }
}
